//
//  CXBlockTestView.swift
//  CXSwiftDemo
//
//  Created by 潮汐 on 2023/12/7.
//闭包传值传值

import UIKit

class CXBlockTestView: UIView {

    //声明一个闭包（无返回值），必须用？号
    var sendValue:(() -> ())?
    
    //声明一个闭包（多返回值），必须用？号
    var getHeaderHeightBlock:((_ headerHeight: Double, _ name: String)->())?
    
    
    //函数参数闭包
//    typealias swiftBlock = (_ str: String, _ height: Double) -> Void
    //写法2
    //函数参数闭包
    typealias swiftBlock = (_ str: String, _ height: Double) -> ()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加数据到UI上
    func addDataToUI() {
        //回调（有参数）一定要用？，不要用系统提示的！
        if self.getHeaderHeightBlock != nil {
            self.getHeaderHeightBlock?(200, "潮汐")
        }
        //回调（无参数） 一定要用？，不要用系统提示的！
        if self.sendValue != nil {
            self.sendValue?()
        }
        
        
        
    }
    
    //闭包：函数参数
    func callBackBlock(_ block: @escaping (_ headerHeight: Double, _ name: String)->()) {
        //回调属性闭包
        self.getHeaderHeightBlock = {[weak self] (headerHeight: Double, name: String) -> () in
            block(headerHeight, name)
        }
        
    }
    
    
    //闭包：函数参数
    func callBackBlockTwo(_ block: @escaping swiftBlock) {
        block("函数参数闭包传值2", 10000)
    }
    
    
    func loadData(title:String, complete : (String) -> ()) {
        // 回调闭包
       complete("收到闭包参数\(title)")
        
    }
    
}
